package com.web.lqj;

import com.dao.BaseDao;
import com.entity.PageManager;
import com.entity.User;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class UserService_lqj extends BaseDao {

    /*public Object[] getObjArr(User user){
        Object[] params = {user.getUsername(), user.getPassword(), user.getName(),
        user.getSex(), user.getAge(), user.getBirthday(), user.getPhone(), user.getHeight(),
        user.getPic()};
        return params;
    }*/

        /**
         * 增加用户数据
         * @param user
         * @return
         */
        public boolean addUser(User user) {
        /*String sql = "INSERT INTO `rz1217`.`user`(`username`, `password`, " +
                "`name`, `sex`, `age`, `birthday`, `phone`, " +
                "`height`, `pic`) " +
                "VALUES (?,?,?,?,?,?,?,?,?)";
        Object[] params = getObjArr(user);*/
            try {
                SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
                sdf.applyPattern("yyyy-MM-dd ");// a为am/pm的标记
                Date date = new Date();// 获取当前时间
                user.setAdministrators("F");
                user.setSupertube("F");
                user.setNumber(5);
                user.setRegister(sdf.format(date));
                user.setLevel("普通商家");
                return insert(user) > 0;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        /**
         * 分页查询
         * @param pm
         * @return
         */
        public List<User> queryUsers(PageManager pm) {
            int start = (pm.getPn() - 1) * pm.getRn();
            int end = pm.getRn();
            String sql = "select * from user limit ?,?";
            return findList(User.class, sql, start, end);
        }

        /**
         * 分页查询
         * @return
         */
        public List<User> queryUsers() {
            String sql = "select * from user";
            return findList(User.class, sql);
        }

        public boolean delUser(int id) {
//      String sql = "delete from user where id = ?";
            User user = queryUser(id);
            boolean result = false;
            try {
                result = delete(new User(id))> 0;
                if (result) {
                    //删除图片
                    new File(user.getPic()).delete();
                }
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public User queryUser(int id) {
//        String sql = "select * from user where id = ?";
            try {
                return (User) query(new User(id));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        public boolean updateUser(User user) {
       /* String sql = "UPDATE `rz1217`.`user` SET `username` = ?, `password` = ?," +
                " `name` = ?, `sex` = ?, `age` = ?, `birthday` = ?, `phone` = ?, " +
                "`height` = ?, `pic` = ? WHERE `id` = ?";
        Object[] params = {user.getUsername(), user.getPassword(), user.getName(),
                user.getSex(), user.getAge(), user.getBirthday(), user.getPhone(), user.getHeight(),
                user.getPic(), user.getId()};*/
            //查询单条数据
            User queryUser = queryUser(user.getId());
            try {
                boolean result = update(user) > 0;
                //修改成功，并且传递的图片和数据库的图片不同时，才删除图片文件
                if (result && queryUser.getPic() != null
                        && !queryUser.getPic().equals(user.getPic())) {
                    //删除原图片
                    new File(queryUser.getPic()).delete();
                }
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public int queryUserTotalCount() {
            String sql = "select count(*) from user";
            return count(sql);
        }

        /**
         * 通过用户名查询用户数据
         * @param username 要查询的用户名
         * @return
         */
        public User queryUserByUsername(String username) {
            String sql = "select * from user where username = ?";
            return findBean(User.class, sql, username);
        }

//    public static void main(String[] args) {
//            //HH:mm:ss a
//        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
//        sdf.applyPattern("yyyy-MM-dd ");// a为am/pm的标记
//        Date date = new Date();// 获取当前时间
//        System.out.println("现在时间：" + sdf.format(date)); // 输出已经格式化的现在时间（24小时制）
//    }
    }


